/*
 * Duplicate Detector Copyright (C) 2010 Marco Biscaro <marcobiscaro2112@gmail.com>
 * 
 * This file is part of Duplicate Detector.
 *
 * Duplicate Detector is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Duplicate Detector is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Duplicate Detector.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.duplicatedetector.view;

import static com.googlecode.duplicatedetector.i18n.Messages._;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import net.miginfocom.swing.MigLayout;

import com.googlecode.duplicatedetector.Constants;
import com.googlecode.duplicatedetector.controller.AddFileActionListener;
import com.googlecode.duplicatedetector.controller.RemoveFileActionListener;
import com.googlecode.duplicatedetector.controller.StartActionListener;
import com.googlecode.duplicatedetector.i18n.Keys;
import com.googlecode.duplicatedetector.model.FileListModel;
import com.googlecode.duplicatedetector.model.Filter;
import com.googlecode.duplicatedetector.model.RegexFilter;

/**
 * The main panel of the application.
 * 
 * @author Marco Biscaro
 */
public class MainPanel extends JPanel implements Constants, Keys {

	private static final long serialVersionUID = -5735335078045123957L;
	private JList fileList;
	private FileListModel fileListModel;
	private JTextField includeTextField;
	private JTextField excludeTextField;
	private JButton removeButton;
	private JButton addButton;
	private JButton startButton;
	private JFileChooser fileChooser;

	public MainPanel() {
		initialize();
	}

	private void initialize() {
		setLayout(new MigLayout(null, "[grow]", "[][grow][]"));
		add(new JLabel(_(DIRS_TO_INCLUDE)), "wrap");
		add(new JScrollPane(getFileList()), "grow, wrap 10");
		add(new JLabel(_(INCLUDE)), "split 4");
		add(getIncludeTextField(), "growx");
		add(new JLabel(_(EXCLUDE)));
		add(getExcludeTextField(), "growx, wrap");
		add(getRemoveButton(), "split 3, align right");
		add(getAddButton());
		add(getStartButton());
	}

	JList getFileList() {
		if (fileList == null) {
			fileList = new JList();
			fileList.setModel(getFileListModel());
			fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		return fileList;
	}

	private JTextField getIncludeTextField() {
		if (includeTextField == null) {
			includeTextField = new JTextField();
			includeTextField.setText("*");
			includeTextField.setToolTipText(_(FILTER_TOOL_TIP));
		}
		return includeTextField;
	}

	private JTextField getExcludeTextField() {
		if (excludeTextField == null) {
			excludeTextField = new JTextField();
			excludeTextField.setText("*.svn-base, *.lnk, *.class");
			excludeTextField.setToolTipText(_(FILTER_TOOL_TIP));
		}
		return excludeTextField;
	}

	private JButton getRemoveButton() {
		if (removeButton == null) {
			removeButton = new JButton();
			removeButton.setText(_(REMOVE));
			removeButton.addActionListener(new RemoveFileActionListener(
					getFileListModel(), getFileList()));
		}
		return removeButton;
	}

	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton();
			addButton.setText(_(ADD));
			addButton.addActionListener(new AddFileActionListener(
					getFileListModel(), getFileChooser(), this));
		}
		return addButton;
	}

	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setText(_(START));
			startButton.addActionListener(new StartActionListener(this));
		}
		return startButton;
	}

	JFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
		return fileChooser;
	}

	public FileListModel getFileListModel() {
		if (fileListModel == null) {
			fileListModel = new FileListModel();
		}
		return fileListModel;
	}

	public Filter getFilter() {
		return new RegexFilter(includeTextField.getText(), excludeTextField
				.getText());
	}
}
